<?php

//新鲜事模块
class Fresh_model extends Model {

	function Fresh_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("site_model","sm");
	}
	
	//添加
	/*
	//数据格式
	$data = array(
		'typeid' => 1,//显示类型 1：站logo+标题||2：站长头像+站长名+内容
		'title' => p("title"),
		'userid' => $this->userid,
		'username' => $this->userinfo['username'],
		'userphoto' => $this->userinfo['photo'],
		'siteid' => $this->site['id'],
		'sitename' => $this->site['name'],
		'sitelogo' => $this->site['info']['logo'],
		'action' => 'add',
		'link' => '/site/'.$this->site['domain'].'/show/'.$this->moduleid.'/'.$result,
		'addtime' => time(),
	);*/
	function add($data)
	{
		$result = $this->db->insert('fresh', $data);
		return $result;
	}
	
	//设置已读
	function set_read($userid)
	{
		$time = time();
		$data = array(
			"userid" => $userid,
			"readtime" => $time,
			"addtime" => $time,
		);
		$result = $this->db->insert("fresh_hack", $data);
		return $result;
	}
	
	//获取fresh类型
	//社区9，广播10
	function get_type($moduleid)
	{
		$typeid = 0;
		if (in_array($moduleid, array(1,2,4,5,6,9))) {//默认
			$typeid = 1;
		} elseif (in_array($moduleid, array(7,8))) {//视频+相册
			$typeid = 2;
		} elseif (in_array($moduleid, array(10))) {//广播
			$typeid = 3;
		}
		return $typeid;
	}
	
	function get_typestr($moduleid)
	{
		$typestr = "";
		switch ($moduleid) {
			case 7:
				$typestr = "视频";
			break;
			case 8:
				$typestr = "相册";
			break;
		}
		return $typestr;
	}
	
	//#获取列表
	//注意防止siteid溢出
	function lists($userid, $limit = 200)
	{
		$lists = $siteids = array();
		//获取关注的小站
		$query = $this->db->get_where("fans", array('userid'=>$userid));
		if ($query->num_rows()>0) {
			$sites = $query->result_array();
			foreach ($sites as $site) {
				$siteids[] = $site['siteid'];
			}
			
			//获取新鲜事hack
			$readtime = 0;
			$q = $this->db->from("fresh_hack")->where("userid", $userid)->order_by("id","desc")->limit(1)->get();
			if ($q->num_rows()>0) {
				$row = $q->row_array();
				$readtime = $row["readtime"];
			}
			
			//获取新鲜事
			$query = $this->db->from("fresh")->where_in("siteid",$siteids)->where("addtime >",$readtime)->order_by("id","desc")->limit($limit)->get();
//			echo $this->db->last_query();exit;
			if ($query->num_rows()>0) {
				$lists = $query->result_array();
				foreach ($lists as $k => $list){
					$site = $this->sm->get_site($list["siteid"]);
					$lists[$k]['domain'] = $site['domain'];
				}
			}
		}
//		a($lists);
		return $lists;
	}
	
	
}

/* End of file */